namespace TLang.Values.Primitives
{
    using Ast;
    using System.Collections.Generic;

    public class LtE : PrimFun
    {

        public LtE() : base("<=", 2)
        {
            
        }

        public override Value Apply(List<Value> args, Node location)
        {

            Value v1 = args[0];
            Value v2 = args[1];
            if (v1 is IntValue && v2 is IntValue)
            {
                return new BoolValue(((IntValue)v1).Value <= ((IntValue)v2).Value);
            }
            if (v1 is FloatValue && v2 is FloatValue)
            {
                return new BoolValue(((FloatValue)v1).Value <= ((FloatValue)v2).Value);
            }
            if (v1 is FloatValue && v2 is IntValue)
            {
                return new BoolValue(((FloatValue)v1).Value <= ((IntValue)v2).Value);
            }
            if (v1 is IntValue && v2 is FloatValue)
            {
                return new BoolValue(((IntValue)v1).Value <= ((FloatValue)v2).Value);
            }

            Util.Abort(location, "incorrect argument types for <=: " + v1 + ", " + v2);
            return null;
        }


        public override Value Typecheck(List<Value> args, Node location)
        {
            Value v1 = args[0];
            Value v2 = args[1];

            if (!(v1 is IntType || v1 is FloatValue) ||
                    !(v2 is IntType || v2 is FloatValue))
            {
                Util.Abort(location, "incorrect argument types for <=: " + v1 + ", " + v2);
            }

            return Type.BOOL;
        }

    }
}